package com.dhcc.bpm.modules.metadata.wrapper;

import com.dhcc.bpm.common.cache.SysCache;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.metadata.entity.Metadata;
import com.dhcc.bpm.modules.metadata.service.impl.MetadataServiceImpl;
import com.dhcc.bpm.modules.metadata.vo.MetadataVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MetadataWrapper
 * @Description 包装类,返回视图层所需的字段
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
public class MetadataWrapper extends BaseEntityWrapper<Metadata, MetadataVO> {
	@Autowired
	private MetadataServiceImpl metadataService;

	public static MetadataWrapper build() {
		return new MetadataWrapper();
	}

	@Override
	public MetadataVO entityVO(Metadata metadata) {
		MetadataVO metadataVO = BeanUtil.copy(metadata, MetadataVO.class);
		assert metadataVO != null;
		if (Func.isEquals(metadata.getParentId(), BladeConstant.TOP_PARENT_ID)) {
			metadataVO.setParentName(BladeConstant.TOP_PARENT_NAME);
		} else {
			Metadata parent = metadataService.getById(metadata.getParentId());
			if(parent != null){
				metadataVO.setParentName(parent.getFieldName());
			}
		}
		return metadataVO;
	}

	public List<MetadataVO> listNodeVO(List<Metadata> list) {
		List<MetadataVO> collect = list.stream().map(metadata -> BeanUtil.copy(metadata, MetadataVO.class)).collect(Collectors.toList());
		for (MetadataVO i : collect){
			i.setNo(i.getId().toString());
			i.setParentNo(i.getParentId().toString());
			if(i.getFieldValid() != null && !"".equals(i.getFieldValid()) && !StringUtils.isEmpty(i.getFieldValid())){
				Long verifyRuleId = Long.parseLong(i.getFieldValid());
				i.setVerifyRuleName(SysCache.getVerifyRule(verifyRuleId).getVerifyRuleName());
			}else{
				i.setVerifyRuleName("");
			}
		}
		return ForestNodeMerger.merge(collect);
	}
	public List<MetadataVO> listNodeVOs(List<MetadataVO> list) {
		for (MetadataVO i : list){
			i.setNo(i.getId().toString());
			i.setParentNo(i.getParentId().toString());
		}
		return ForestNodeMerger.merge(list);
	}


}
